import apiClient from './api'

// RAG上传任务状态接口
export interface UploadStatus {
  task_id: string
  status: 'pending' | 'processing' | 'completed' | 'failed'
  progress: number
  message?: string
  document_id?: string
}

// RAG服务类
class RagService {
  // 上传文档到RAG知识库
  async uploadDocument(file: File, collectionId?: string): Promise<{ task_id: string }> {
    const formData = new FormData()
    formData.append('file', file)
    // 使用文件名作为标题
    formData.append('title', file.name)
    if (collectionId) {
      formData.append('collection_id', collectionId)
    }

    const response = await apiClient.post('/v1/rag/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    return response.data
  }

  // 获取上传状态
  async getUploadStatus(taskId: string): Promise<UploadStatus> {
    const response = await apiClient.get(`/v1/rag/upload/status/${taskId}`)
    return response.data
  }

  // 取消上传任务
  async cancelUpload(taskId: string): Promise<void> {
    await apiClient.post(`/v1/rag/upload/cancel/${taskId}`)
  }
}

export default new RagService()